package com.newture.lxz.controller;

import com.newture.lxz.controller.helper.Result;
import com.newture.lxz.generator.output.Column;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;

@Controller
@RequestMapping("/caseAcceptanceFees")
@Validated
public class CourtFeesCalculator {
    public static void main(String[] args) {
        CourtFeesCalculator cal=new CourtFeesCalculator();
        double a=5;
        System.out.println(cal.max(-600,a-Double.POSITIVE_INFINITY));
    }
    @GetMapping("/calc")
    @ResponseBody
    public Result calc(
            @RequestParam(name = "caseType", required = true) String caseType,
            @RequestParam(name = "caseObject", required = true) String caseObject){
            System.out.println("caseType: "+caseType+"caseObject: "+caseObject);
            double caseFee=Double.parseDouble(caseObject);
            Emm emm=new Emm();

            switch(caseType){
                case "财产案件":{
                    double temp= 50+max(caseFee-10000,caseFee-100000)*0.025+max(caseFee-100000,caseFee-200000)*0.02
                            +max(caseFee-200000,caseFee-500000)*0.015+max(caseFee-500000,caseFee-1000000)*0.01+max(caseFee-1000000,caseFee-2000000)*0.009
                            +max(caseFee-2000000,caseFee-5000000)*0.008+max(caseFee-5000000,caseFee-10000000)*0.007+max(caseFee-10000000,caseFee-20000000)*0.006+
                            max(caseFee-20000000,caseFee-Double.POSITIVE_INFINITY)*0.005;
                    BigDecimal b = new BigDecimal(temp);
                    temp = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    emm.setFee(new double[]{temp,temp});
                    break;
                }
                case "离婚案件":emm.setFee(new double[]{50,300});break;
                case "人格权案件":emm.setFee(new double[]{300,500});break;
                case "其他非财产案件":emm.setFee(new double[]{50,100});break;
                case "知识产权案件":emm.setFee(new double[]{500,1000});break;
                case "支付令申请": {
                    double temp1= 50+max(caseFee-10000,caseFee-100000)*0.025+max(caseFee-100000,caseFee-200000)*0.02
                            +max(caseFee-200000,caseFee-500000)*0.015+max(caseFee-500000,caseFee-1000000)*0.01+max(caseFee-1000000,caseFee-2000000)*0.009
                            +max(caseFee-2000000,caseFee-5000000)*0.008+max(caseFee-5000000,caseFee-10000000)*0.007+max(caseFee-10000000,caseFee-20000000)*0.006+
                            max(caseFee-20000000,caseFee-Double.POSITIVE_INFINITY)*0.005;
                    temp1/=3;
                    temp1=Math.round(temp1);
                    BigDecimal b=new BigDecimal(temp1);
                    temp1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    emm.setFee(new double[]{temp1,temp1});
                    break;
                }
                case "破产案件":{
                    double temp1= 50+max(caseFee-10000,caseFee-100000)*0.025+max(caseFee-100000,caseFee-200000)*0.02
                            +max(caseFee-200000,caseFee-500000)*0.015+max(caseFee-500000,caseFee-1000000)*0.01+max(caseFee-1000000,caseFee-2000000)*0.009
                            +max(caseFee-2000000,caseFee-5000000)*0.008+max(caseFee-5000000,caseFee-10000000)*0.007+max(caseFee-10000000,caseFee-20000000)*0.006+
                            max(caseFee-20000000,caseFee-Double.POSITIVE_INFINITY)*0.005;
                    temp1/=2;
                    temp1=Math.round(temp1);
                    temp1=Math.min(temp1,300000);
                    BigDecimal b=new BigDecimal(temp1);
                    temp1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    emm.setFee(new double[]{temp1,temp1});
                    break;
                }
                case "劳动争议案件":emm.setFee(new double[]{10,10});break;
                case "行政案件":emm.setFee(new double[]{100,100});break;
                case "管辖权异议案件":{
                    emm.setFee(new double[]{0,100});
                    emm.setFeedesc("（异议成立，不收费；异议不成立，收费100元/件）");
                    break;
                }
                case "公示催告申请":emm.setFee(new double[]{100,100});break;
                case "撤销仲裁裁决或认定仲裁协议效力申请":emm.setFee(new double[]{400,400});break;
                case "海事案件申请":emm.setFee(new double[]{100,100});break;
                default:emm.setFee(new double[]{caseFee-Double.POSITIVE_INFINITY,caseFee-Double.POSITIVE_INFINITY});break;
            }

            emm.setPreservation(new BigDecimal(Math.min(Math.round(30+max(caseFee-1000,caseFee-100000)*0.01+max(caseFee-100000,caseFee-Double.POSITIVE_INFINITY)*0.005), 5000)).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());

            double exFee=50+max(caseFee-10000,caseFee-500000)*0.015+max(caseFee-500000,caseFee-5000000)*0.01
                    +max(caseFee-5000000,caseFee-10000000)*0.005+max(caseFee-10000000,caseFee-Double.POSITIVE_INFINITY)*0.001;
            BigDecimal b=new BigDecimal(exFee);
            exFee = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            emm.setExecution(new double[]{exFee,exFee});
            Result result=Result.success(emm);
            return result;
    }
    public double max(double a,double b){
        double temp=(a>=b?a:b);
        double temp1=(temp>=0?temp:0);
        return temp1<=(a-b)?temp1:(a-b);
    }
}
